package de.g18.llmaa.gui.workspace.internalFrame.product.key.verleihVerwaltung;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import de.g18.llmaa.gui.App_LLMAA_Controler;
import de.g18.llmaa.gui.IApp_Database_Model;
import de.g18.llmaa.logic.db.HSQLDBConnect;
import de.g18.llmaa.logic.vos.VOKey;
import de.g18.llmaa.logic.vos.VOPerson;
import de.g18.llmaa.logic.vos.VOPerson_Schueler;
import de.g18.llmaa.logic.vos.VOSchulKlasse;

public class User_Suchen extends JPanel {

	private JPanel panel_Suchoption;
	private JTextField textField_Vorname;
	private JTextField textField_Nachname;
	private JComboBox comboBox_Klasse;

	private JButton button_Suchen;

	private JScrollPane scrollPane_SuchErg;
	private JTable table_SuchErg;
	private TableModel_Person tableModel_Person;
	
	private JPanel panel_Button;
	private JButton button_Zuweisen;

	private VOKey correntKey;
	
	public User_Suchen(VOKey correntKey) {
		this.correntKey = correntKey;
		initPanel();
	}

	private void initPanel() {
		this.setLayout(new BorderLayout());
		this.add(getPanel_Suchoption(), BorderLayout.NORTH);
		this.add(getScrollPane_SuchErg(), BorderLayout.CENTER);
		this.add(getPanel_Button(),BorderLayout.SOUTH);
	}

	public JPanel getPanel_Suchoption() {
		if (panel_Suchoption == null) {
			panel_Suchoption = new JPanel();
			panel_Suchoption.setLayout(new GridLayout(2, 3));
			panel_Suchoption.add(new JLabel("Vorname :"));
			panel_Suchoption.add(getTextField_Vorname());
			panel_Suchoption.add(new JLabel("Nachname"));
			panel_Suchoption.add(getTextField_Nachname());
			panel_Suchoption.add(new JLabel("Klasse"));
			panel_Suchoption.add(getComboBox_Klasse());
			panel_Suchoption.add(new JLabel());
			panel_Suchoption.add(getButton_Suchen());
		}
		return panel_Suchoption;
	}

	public JTextField getTextField_Vorname() {
		if (textField_Vorname == null) {
			textField_Vorname = new JTextField();
		}
		return textField_Vorname;
	}

	public JTextField getTextField_Nachname() {
		if (textField_Nachname == null) {
			textField_Nachname = new JTextField();
		}
		return textField_Nachname;
	}

	public JComboBox getComboBox_Klasse() {
		if (comboBox_Klasse == null) {
			ArrayList<VOSchulKlasse> erg = ((IApp_Database_Model) App_LLMAA_Controler
					.getInstanze().getModel()).getSchulKlassen();

			comboBox_Klasse = new JComboBox(erg.toArray());
			comboBox_Klasse.addItem(null);
			comboBox_Klasse.setSelectedItem(null);
		}
		return comboBox_Klasse;
	}

	public JScrollPane getScrollPane_SuchErg() {
		if (scrollPane_SuchErg == null) {
			scrollPane_SuchErg = new JScrollPane();
			scrollPane_SuchErg.setViewportView(getTable_SuchErg());
		}
		return scrollPane_SuchErg;
	}

	public JTable getTable_SuchErg() {
		if (table_SuchErg == null) {
			table_SuchErg = new JTable();
			table_SuchErg.setSelectionMode(0);
			table_SuchErg.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
				
				@Override
				public void valueChanged(ListSelectionEvent arg0) {
					getButton_Zuweisen().setEnabled(true);
					
				}
			});
		}
		return table_SuchErg;
	}

	public JButton getButton_Suchen() {
		if (button_Suchen == null) {
			button_Suchen = new JButton("Suchen");
			button_Suchen.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					ArrayList<VOPerson> suchErgPerson;
					if (getComboBox_Klasse().getSelectedItem() != null) {
						ArrayList<VOPerson_Schueler> suchErgSchueler = HSQLDBConnect
								.getInstanze().selectSchueler(
										getTextField_Vorname().getText(),
										getTextField_Nachname().getText(),
										((VOSchulKlasse) (getComboBox_Klasse()
												.getSelectedItem()))
												.getKlassenId());

						suchErgPerson = new ArrayList<VOPerson>(suchErgSchueler);
					} else {
						ArrayList<VOPerson_Schueler> suchErg = HSQLDBConnect
								.getInstanze().selectSchueler(
										getTextField_Vorname().getText(),
										getTextField_Nachname().getText(),0);

						 suchErgPerson = HSQLDBConnect
						.getInstanze().selectPerson(
								getTextField_Vorname().getText(),
								getTextField_Nachname().getText());
						
						System.out.println(suchErg.size());
						
						suchErgPerson.addAll(suchErg);
						
					}
					tableModel_Person = new TableModel_Person(suchErgPerson);
					table_SuchErg.setModel(tableModel_Person);
					getButton_Zuweisen().setEnabled(false);

				}
			});
		}
		return button_Suchen;
	}

	public JPanel getPanel_Button() {
		if(panel_Button== null)
		{
			panel_Button = new JPanel();
			panel_Button.add(getButton_Zuweisen());
		}
		return panel_Button;
	}

	public JButton getButton_Zuweisen() {
		if(button_Zuweisen == null)
			{
			button_Zuweisen = new JButton("Zuweisen");
			button_Zuweisen.setEnabled(false);
			button_Zuweisen.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					
				VOPerson person = tableModel_Person.getValue(table_SuchErg.getSelectedRow());
				
				if(!HSQLDBConnect.getInstanze().checkPersonForKey(correntKey,person))
				{
					HSQLDBConnect.getInstanze().updateKeyUser(correntKey,person);
				}else{
					JOptionPane.showMessageDialog(new JFrame(),
						    "Die Person besitzt bereits einen Lizenz dieses Produktes",
						    "Keine Doppelvergabe!",
						    JOptionPane.ERROR_MESSAGE);

				}
				
				
				System.out.println(person.getVorName());
					
				}
			});
			}
		return button_Zuweisen;
	}

	
	
	
}
